﻿
@{
    ViewBag.Title = "库存管理";
}

<div class="row col-md-12 ml-0" style="height:5vh;display:table-cell;vertical-align:middle">
    <strong class="font-rfs">库存管理</strong>
</div>
<div class="row col-md-12 ml-0 pre-scrollable" style="min-height:90vh;">
    <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-3" style="min-height:90vh;">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;height: 3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li id="li-tabMaterial" class="tabButton col-md-3 tab-first-active font-rfs" style="text-align:center;padding-left: 1px;padding-right: 1px;" data-active="true">
                    <a id="a-tabMaterial" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">物料列表</a>
                </li>
                <li class="col-md-9 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="row col-md-12 tab-content tab-body" style="padding-right: 0px;right: -15px;padding-left: 0px;top: -2px;min-height: 87vh;">
            <div class="col-md-12 ml-0 mr-0 tab-pane active" style="padding-left: 0px;padding-right: 0px;">
                <div class="row col-md-12" style="margin-left: 0px;padding-left: 1px;">
                    <select id="InvMaterial-Select-Type1" class="form-control font-rfs" style="width:38%; margin-top:0.2rem; margin-bottom:0.1rem; height: calc(1.3em + .75rem + 2px);"></select>
                    <select id="InvMaterial-Select-Type2" class="form-control font-rfs" style="width:37%; margin-top:0.2rem; margin-bottom:0.1rem; height: calc(1.3em + .75rem + 2px);"></select>
                    <label class=" font-rfs" style="width:25%; margin-top:0.5rem; padding-left: 1rem;" title="显示所有物料">
                        <input type="checkbox" id="InvMaterial-all" class="font-rfs" style="width:18%"/>
                        全部
                    </label>
                </div>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12">
                    <table id="tabMaterial" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>物料编码</th>
                                <th>物料名称</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row rounded col-md-9 ml-1" style="min-height:90vh;">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;height: 3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li id="li-InventoryInfo" class="tabButton col-md-1 tab-first-active font-rfs" style="text-align:center;padding-left: 1px;padding-right: 1px;" data-active="true">
                    <a id="a-InventoryInfo" href="#tabInventoryInfo" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">库存信息</a>
                </li>
                <li id="li-tabHistory" class="tabButton col-md-1 tab-other-noactive font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;" data-active="false">
                    <a id="a-tabHistory" href="#tabHistory" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">历史记录</a>
                </li>
                <li class="col-md-10 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="row col-md-12 tab-content tab-body" style="padding-right: 0px;right: -15px;padding-left: 0px;top: -2px;min-height: 87vh;">
            <div class="col-md-12 ml-0 mr-0 tab-pane active" id="tabInventoryInfo" style="padding-left: 0px;padding-right: 0px;">
                <div class="row col-md-12" style="height:1vh"></div>
                <ul class="nav col-md-12" style="margin-bottom: 1px;padding-right: 0px;">
                    <li class="tabButton col-md-1 tab-other-active font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;height:3vh;" data-active="false">
                        <a href="#tabInventoryInfo" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">入库单</a>
                    </li>
                    <li class="col-md-11 tab-Notactive" style="text-align: center;height:3vh;"><a href="" data-toggle="tab"></a></li>
                </ul>
                <div class="row col-md-12" style="min-height:5vh;">
                    <button id="AddEntryInventory" class="btn btn-rfs"><i class="bi-database-fill-add"></i> 入库</button>
                    <button id="EditEntryInventory" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
                    <button id="DelEntryInventory" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
                    <button id="LockEntryInventory" class="btn btn-rfs"><i class="bi-database-fill-lock"></i> 锁定/解锁</button>
                    @* <button id="MergeEntryInventory" class="btn btn-rfs"><i class="bi-database-fill-up"></i> 合并</button> *@
                </div>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="height:24vh;padding-right: 0px;margin-left: 0px;border-bottom: 1px solid #a8bfde;margin-bottom: 5px;">
                    <table id="tabEntryInventory" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>状态</th>
                                <th>入库单号</th>
                                <th>入库数量</th>
                                <th>入库单位</th>
                                <th>可领数量</th>
                                <th>可领单位</th>
                                <th>剩余可领</th>
                                <th>入库时间</th>
                                <th>入库人</th>
                                <th>有效期至</th>
                                <th>单价</th>
                                <th>总价</th>
                                <th>提醒</th>
                                <th>存储</th>
                                <th>条件</th>
                                <th>备注</th>
                                <th>当前步骤</th>
                                <th>下一步骤</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
                <ul class="nav col-md-12" style="margin-bottom: 1px;padding-right: 0px;">
                    <li class="tabButton col-md-1 tab-other-active font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;height:3vh;" data-active="false">
                        <a href="" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">出库单</a>
                    </li>
                    <li class="col-md-11 tab-Notactive" style="text-align: center;height:3vh;"><a href="" data-toggle="tab"></a></li>
                </ul>
                <div class="row col-md-12" style="min-height:5vh;">
                    <button id="AddOutInventory" class="btn btn-rfs"><i class="bi-database-fill-dash" style="color:forestgreen"></i> 出库</button>
                    <button id="EditOutInventory" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
                    <button id="ReturnOutInventory" class="btn btn-rfs"><i class="bi-reply-fill" style="color:red"></i> 退库</button>
                    <button id="UseOutInventory" class="btn btn-rfs"><i class="bi-check2-square" style="color:forestgreen"></i> 领用</button>
                    <button id="OutInventoryReview" class="btn btn-rfs"><i class="bi-person-check-fill" style="color:red"></i> 审核</button>
                    <button id="DelOutInventory" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
                    @* <button id="MergeOutInventory" class="btn btn-rfs"><i class="bi-database-fill-up"></i> 合并</button> *@
                </div>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="height:45vh;">
                    <table id="tabOutInventory" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>状态</th>
                                <th>出库单号</th>
                                <th>出库数量</th>
                                <th>出库单位</th>
                                <th>可领数量</th>
                                <th>可领单位</th>
                                <th>剩余可领</th>
                                <th>出库时间</th>
                                <th>出库人</th>
                                <th>有效期至</th>
                                <th>单价</th>
                                <th>总价</th>
                                <th>提醒</th>
                                <th>存储</th>
                                <th>条件</th>
                                <th>备注</th>
                                <th>领用</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
            <div class="col-md-12 ml-0 mr-0 tab-pane" id="tabHistory" style="padding-left: 0px;padding-right: 0px;">
                <div class="row col-md-12" style="height:1vh"></div>
                <ul class="nav col-md-12" style="margin-bottom: 1px;padding-right: 0px;">
                    <li class="tabButton col-md-1 tab-other-active font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;height:3vh;" data-active="false">
                        <a href="#tabInventoryInfo" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">入库单</a>
                    </li>
                    <li class="col-md-11 tab-Notactive" style="text-align: center;height:3vh;"><a href="" data-toggle="tab"></a></li>
                </ul>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:24vh;padding-right: 0px;margin-left: 0px;border-bottom: 1px solid #a8bfde;margin-bottom: 5px;">
                    <table id="tabEntryInventoryHistory" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>入库单号</th>
                                <th>入库数量</th>
                                <th>入库单位</th>
                                <th>可领数量</th>
                                <th>可领单位</th>
                                <th>入库时间</th>
                                <th>入库人</th>
                                <th>核销时间</th>
                                <th>备注</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
                <ul class="nav col-md-12" style="margin-bottom: 1px;padding-right: 0px;">
                    <li class="tabButton col-md-1 tab-other-active font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;height:3vh;" data-active="false">
                        <a href="" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">出库单</a>
                    </li>
                    <li class="col-md-11 tab-Notactive" style="text-align: center;height:3vh;"><a href="" data-toggle="tab"></a></li>
                </ul>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="height:47vh;">
                    <table id="tabOutInventoryHistory" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>出库单号</th>
                                <th>出库数量</th>
                                <th>出库单位</th>
                                <th>可领数量</th>
                                <th>可领单位</th>
                                <th>出库时间</th>
                                <th>出库人</th>
                                <th>备注</th>
                                <th>领用</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal" id="InvManager-Add-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                @* <label id="SAMPLE-addMaterial-ModalID" style="display:none">InvManager-Add-Modal</label> *@
            </div>
            <div class="modal-body">
                <div id="InvManager-Add-Modal-Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="InvManager-Remove-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="InvManager-Remove-Modal-Title" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p id="InvManager-Remove-Modal-body" class="font-rfs"></p>
            </div>
            <div class="modal-Delete-footer">
                <button id="InvManager-Remove-Modal-Entryconfirm" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">确定</button>
                <button id="InvManager-Remove-Modal-Outconfirm" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">确定</button>
                <button id="InvManager-Remove-Modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="InvManager-Use-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="InvManager-Use-modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <div class="col-3 pr-0">
                        <span class="input-group-text font-rfs border-radius-noright noRightBorder">领用数量*</span>
                    </div>
                    <div class="col-9 pl-0 input-group">
                        <input id="InvManager-Use-Modal-Quantity" class="form-control font-rfs border-radius-noleft" placeholder="必需是有效数字" />
                        <button id="InvManager-Use-Modal-Unit" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%"></button>
                    </div>
                </div>
                <div class="input-group">
                    <div class="col-3 pr-0">
                        <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
                    </div>
                    <div class="col-9 pl-0 input-group">
                        <input id="InvManager-Use-Modal-Note" class="form-control font-rfs border-radius-noleft" placeholder="非必填" />
                    </div>
                </div>
            </div>
            <div class="modal-Delete-footer">
                <button id="InvManager-Use-Modal-confirm" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">确定</button>
                <button id="InvManager-Use-Modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var materialCode = '';
        var materialName = '';
        var materialIsEnable = false;
        var EntryInventoryId = '';
        var EntryInventoryHistoryId = '';
        var OutInventoryId = '';

        $(document).ready(function () {
            LoadType1()
        });

        function LoadType1() {
            $.post("GetMaterialType1", {
            }, function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#InvMaterial-Select-Type1").append(new Option(data[i].type1));
                }
                $("#InvMaterial-Select-Type1").prepend(new Option("", ""));
                $("#InvMaterial-Select-Type1").val("");
            });
        }

        function LoadType2() {
            $.post("GetMaterialType2ByType1", {
                type1: $("#InvMaterial-Select-Type1").find("option:selected").text()
            }, function (data) {
                $("#InvMaterial-Select-Type2").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#InvMaterial-Select-Type2").append(new Option(data[i].type2));
                }
                $("#InvMaterial-Select-Type2").prepend(new Option("", ""));
                $("#InvMaterial-Select-Type2").val("");
            });
        }

        $("#InvMaterial-Select-Type1").change(function () {
            LoadType2();
            LoadMateria();
        });

        $("#InvMaterial-Select-Type2").change(function () {
            LoadMateria();
        });

        function LoadMateria() {
            var columns = [{ field: "code" }, { field: "name" },
            { field: "isEnable", width: "5%", align: "center", visible: $("#InvMaterial-all").prop('checked') == true ? true : false, formatter: SetIsEnableCortrol }];

            materialCode = '';
            materialName = '';
            $('#tabMaterial').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabMaterial').bootstrapTable({
                method: 'post',
                url: "GetMaterialListByTypeAndEnable",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        type1: $("#InvMaterial-Select-Type1").find("option:selected").text(),
                        type2: $("#InvMaterial-Select-Type2").find("option:selected").text(),
                        isEnable: $("#InvMaterial-all").prop('checked')
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "code",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 20,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabMaterial").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    materialCode = row.code;
                    materialName = row.name;
                    materialIsEnable = row.isEnable;
                    if ($("#li-InventoryInfo").attr("class").indexOf('tab-first-active') > 0) {
                        LoadEntryInventoryList();
                        $('#tabOutInventory').bootstrapTable('destroy');
                    } else {
                        LoadEntryInventoryHistoryList();
                        $('#tabOutInventoryHistory').bootstrapTable('destroy');
                    }
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabMaterial').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function SetUseRecord(value, row, index) {
            var strHtml = "<button class='table-cell-button btn-rfs' data-Id=" + row.id + "  title='查看领用记录' onclick='ViewUseListClick(this)'><i class='bi-file-text'></i></button>";
            return strHtml;
        }

        function ViewUseListClick(obj) {
            $("#modalTitle").text("领用记录");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("ViewUseRecords", {
                OutInventoryId: $(obj).parent().parent().attr('data-uniqueid'),
                ModalID: "InvManager-Add-Modal"
            }, function (data) {
                $("#InvManager-Add-Modal-Embed").html(data);
            });
        }

        function SetIsEnableCortrol(value, row, index) {
            var strHtml = "";
            if (row.isEnable) {
                strHtml = "<i class='bi bi-check-circle-fill'></i>";
            } else {
                strHtml = "<i class='bi bi-x-circle-fill'></i>";
            }

            return strHtml;
        }

        function LoadEntryInventoryList() {
            var columns = [{ field: "id", visible: false }, { field: "status", width: "5%", align: "center", formatter: SetStatus },
            { field: "inventoryNumber" }, { field: "entryQuantity" }, { field: "entryUnit" },
            { field: "availableQuantity" }, { field: "availableUnit" }, { field: "receivedQuantity" }, { field: "inDate", formatter: setDate },
            { field: "operatorName" }, { field: "vld", formatter: setVld }, { field: "unitPrice" }, { field: "totalPrice" }, { field: "remind" },
            { field: "positionId" }, { field: "storageConditions" }, { field: "note" },
            { field: "currentWorkFlow", visible: false }, { field: "nextWorkFlow", visible: false }];

            EntryInventoryId = '';
            $('#tabEntryInventory').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabEntryInventory').bootstrapTable({
                method: 'post',
                url: "GetEntryListByMaterialCode",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        MaterialCode: materialCode
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabEntryInventory").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    EntryInventoryId = row.id;
                    LoadOutInventoryList();
                    if (row.status == "核销") {
                        $("#LockEntryInventory").attr("disabled", true);
                    } else {
                        $("#LockEntryInventory").attr("disabled", false);
                    }
                    if (row.status == "锁定" || row.status == "核销") {
                        $("#AddOutInventory").attr("disabled", true);
                    } else {
                        $("#AddOutInventory").attr("disabled", false);
                    }
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabEntryInventory').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function LoadEntryInventoryHistoryList() {
            var columns = [{ field: "id", visible: false },{ field: "inventoryNumber" }, { field: "entryQuantity" }, { field: "entryUnit" },
            { field: "availableQuantity" }, { field: "availableUnit" }, { field: "inDate", formatter: setDate },
            { field: "operatorName" }, { field: "cancelDate", formatter: setDate }, { field: "note" }];

            EntryInventoryHistoryId = '';
            $('#tabEntryInventoryHistory').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabEntryInventoryHistory').bootstrapTable({
                method: 'post',
                url: "GetEntryHistoryListByMaterialCode",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        MaterialCode: materialCode
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabEntryInventoryHistory").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    EntryInventoryHistoryId = row.id;
                    LoadOutInventoryHistoryList();
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabEntryInventoryHistory').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function setDate(value, row, index) {
            if (value != null)
                return value.split('T')[0] + " " + (value.split('T')[1]).split('.')[0];
        }

        function setVld(value, row, index) {
            if (value != null)
                return value.split('T')[0];
        }

        function SetStatus(value, row, index) {
            var strHtml = "-";
            if (value == "正常") {
                strHtml = "<i class='bi-unlock-fill' style='color:forestgreen'></i>";
            }
            if (value == "锁定") {
                strHtml = "<i class='bi-lock-fill' style='color:red'></i>";
            }
            if (value == "核销") {
                strHtml = "<i class='bi-check2-square' style='color:forestgreen'></i>";
            }
            return strHtml;
        }

        function LoadOutInventoryList() {
            var columns = [{ field: "id", visible: false }, { field: "status", width: "5%", align: "center", formatter: SetStatus },
            { field: "inventoryNumber" }, { field: "entryQuantity" }, { field: "entryUnit" },
            { field: "availableQuantity" }, { field: "availableUnit" }, { field: "receivedQuantity" }, { field: "inDate", formatter: setDate },
            { field: "operatorName" }, { field: "vld", formatter: setVld }, { field: "unitPrice" }, { field: "totalPrice" }, { field: "remind" },
            { field: "positionId" }, { field: "storageConditions" }, { field: "note" },{ field: "", width: "5%", formatter: SetUseRecord }];

            OutInventoryId = '';
            $('#tabOutInventory').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabOutInventory').bootstrapTable({
                method: 'post',
                url: "GetOutListByEntryId",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        EntryInventorId: EntryInventoryId
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabOutInventory").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    OutInventoryId = row.id;
                    if (row.status == "核销") {
                        $("#EditOutInventory").attr("disabled", true);
                        $("#ReturnOutInventory").attr("disabled", true);
                        $("#UseOutInventory").attr("disabled", true);
                        $("#OutInventoryReview").attr("disabled", true);
                    }
                    if (row.status == "锁定") {
                        $("#UseOutInventory").attr("disabled", true);
                        $("#ReturnOutInventory").attr("disabled", true);
                        $("#DelOutInventory").attr("disabled", true);
                        $("#OutInventoryReview").attr("disabled", false);
                    }
                    if (row.status == "正常") {
                        $("#EditOutInventory").attr("disabled", false);
                        $("#ReturnOutInventory").attr("disabled", false);
                        $("#UseOutInventory").attr("disabled", false);
                        $("#OutInventoryReview").attr("disabled", true);
                        $("#DelOutInventory").attr("disabled", false);
                    }
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabOutInventory').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function LoadOutInventoryHistoryList() {
            var columns = [{ field: "id", visible: false }, { field: "inventoryNumber" }, { field: "entryQuantity" }, { field: "entryUnit" },
            { field: "availableQuantity" }, { field: "availableUnit" }, { field: "inDate", formatter: setDate },
            { field: "operatorName" }, { field: "note" }, { field: "", width: "5%", formatter: SetUseRecord }];

            $('#tabOutInventoryHistory').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabOutInventoryHistory').bootstrapTable({
                method: 'post',
                url: "GetOutListByEntryId",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        EntryInventorId: EntryInventoryHistoryId
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabOutInventoryHistory").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabOutInventoryHistory').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        $("#AddEntryInventory").click(function () {
            if (materialCode.length == 0) {
                alert("请先选择需要入库的物料！");
                return;
            }
            if (materialIsEnable == false) {
                alert("该物料已被停用，不能添加入库单！");
                return;
            } 

            $("#modalTitle").text("添加入库单");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#AddEntryInventory", parent.document.body).attr("src", "AddEntryInventory");
            $.post("AddEntryInventory", {
                MaterialCode: materialCode,
                MaterialName: materialName,
                ModalID: "InvManager-Add-Modal"
            }, function (datas) {
                $("#InvManager-Add-Modal-Embed").html(datas);
            });
        });

        $("#EditEntryInventory").click(function () {
            if (EntryInventoryId.length == 0) {
                alert("请先选择需要编辑的入库单！");
                return;
            }

            $("#modalTitle").text("编辑入库单");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#EditEntryInventory", parent.document.body).attr("src", "AddEntryInventory");
            $.post("AddEntryInventory", {
                MaterialCode: materialCode,
                MaterialName: materialName,
                EntryInventoryId: EntryInventoryId,
                ModalID: "InvManager-Add-Modal"
            }, function (datas) {
                $("#InvManager-Add-Modal-Embed").html(datas);
            });
        });

        $("#DelEntryInventory").click(function () {
            if (EntryInventoryId.length == 0) {
                alert("请先选择需要删除的入库单！");
                return;
            }

            var row = $('#tabEntryInventory').bootstrapTable('getRowByUniqueId', EntryInventoryId);
            $("#InvManager-Remove-Modal-Title").text("删除入库单");
            $("#InvManager-Remove-Modal-Entryconfirm").show();
            $("#InvManager-Remove-Modal-Outconfirm").hide();
            $("#InvManager-Remove-Modal-body").text("该操作将永久删除入库单：" + row.inventoryNumber +" 是否确定？");

            $("#InvManager-Remove-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#InvManager-Remove-Modal-Entryconfirm").click(function () {
            $.post("DelEntryInventory", {
                Id: EntryInventoryId
            }, function (datas) {
                LoadEntryInventoryList();
                $("#InvManager-Remove-Modal-cancel").click();
                alert(datas);
            });
        });

        $("#InvManager-Remove-Modal-Outconfirm").click(function () {
            $.post("DelOutInventory", {
                Id: GetSelectIdByColor('tabOutInventory')
            }, function (datas) {
                LoadOutInventoryList();
            $("#InvManager-Remove-Modal-cancel").click();
                alert(datas);
            });
        });

        $("#LockEntryInventory").click(function () {
            if (EntryInventoryId.length == 0) {
                alert("请先选择需要锁定或解锁的入库单！");
                return;
            }

            $.post("ReOrLockEntryInventory", {
                Id: EntryInventoryId
            }, function (datas) {
                LoadEntryInventoryList();
                alert(datas);
            });
        });

        $("#AddOutInventory").click(function () {
            if (EntryInventoryId.length == 0) {
                alert("请先选择对应的入库单！");
                return;
            }

            $("#modalTitle").text("添加出库单");
            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#AddOutInventory", parent.document.body).attr("src", "AddOutInventory");
            $.post("AddOutInventory", {
                EntryInventoryId: EntryInventoryId,
                ModalID: "InvManager-Add-Modal"
            }, function (datas) {
                $("#InvManager-Add-Modal-Embed").html(datas);
            });
        });

        $("#EditOutInventory").click(function () {
            var outId = GetSelectIdByColor('tabOutInventory');
            if (outId.length == 0) {
                alert("请先选择需要编辑的出库单！");
                return;
            }

            $("#modalTitle").text("编辑出库单");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddOutInventory", {
                OutInventoryId: OutInventoryId,
                ModalID: "InvManager-Add-Modal"
            }, function (datas) {
                $("#InvManager-Add-Modal-Embed").html(datas);
            });
        });

        $("#ReturnOutInventory").click(function () {
            if (OutInventoryId.length == 0) {
                alert("请先选择需要退库的出库单！");
                return;
            }

            $("#modalTitle").text("退库");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("ReturnInventory", {
                OutInventoryId: OutInventoryId,
                ModalID: "InvManager-Add-Modal"
            }, function (datas) {
                $("#InvManager-Add-Modal-Embed").html(datas);
            });
        });

        $("#UseOutInventory").click(function () {
            if (OutInventoryId.length == 0) {
                alert("请先选择需要领用物品的出库单！");
                return;
            }

            $("#InvManager-Use-modalTitle").text("物料领用");
            var row = $('#tabEntryInventory').bootstrapTable('getRowByUniqueId', EntryInventoryId);
            $("#InvManager-Use-Modal-Unit").text(row.availableUnit);

            $("#InvManager-Use-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#OutInventoryReview").click(function () {
            if (OutInventoryId.length == 0) {
                alert("请先选择需要审核的出库单！");
                return;
            }

            $("#modalTitle").text("审批");

            $("#InvManager-Add-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("\\WorkFlow\\ExamineAndApprove", {
                DomainName: "Inventory",
                DomainId: OutInventoryId,
                ModalID: "InvManager-Add-Modal"
            }, function (data) {
                $("#InvManager-Add-Modal-Embed").html(data);
            });
        });

        $("#InvManager-Use-Modal-confirm").click(function () {
            if ($.trim($("#InvManager-Use-Modal-Quantity").val()).length == 0) {
                alert("领用数量不能为空！");
                $("#InvManager-Use-Modal-Quantity").addClass('error-style');
                return false;
            }

            $.post("UseOutInventory", {
                OutInventoryId: OutInventoryId,
                Quantity: $.trim($("#InvManager-Use-Modal-Quantity").val()),
                Note: $.trim($("#InvManager-Use-Modal-Note").val())
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#InvManager-Use-Modal-cancel").click();
                } else {
                    alert(data);
                }
            });
        });

        $("#DelOutInventory").click(function () {
            var outId = GetSelectIdByColor('tabOutInventory');
            if (outId.length == 0) {
                alert("请先选择需要删除的出库单！");
                return;
            }

            $.when(GetUserRolesSt()).then(function (data) {
                if (data.indexOf("Admin") == -1) {
                    alert("删除出库单需要有管理员权限！");
                    return;
                } else {
                    var row = $('#tabOutInventory').bootstrapTable('getRowByUniqueId', outId);
                    $("#InvManager-Remove-Modal-Title").text("删除出库单");
                    $("#InvManager-Remove-Modal-Entryconfirm").hide();
                    $("#InvManager-Remove-Modal-Outconfirm").show();
                    $("#InvManager-Remove-Modal-body").text("该操作将永久删除出库单及相关领用记录：" + row.inventoryNumber + " 是否确定？");

                    $("#InvManager-Remove-Modal").modal({
                        backdrop: 'static',
                        keyboard: false
                    });
                }
            });
        });

//********************************************************************************

        $("#li-InventoryInfo").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-first-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-first-noactive') > 0) {
                    $(this).removeClass('tab-first-noactive');
                    $(this).addClass('tab-first-noactive-mouse');
                    LoadEntryInventoryList();
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-first-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-first-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-first-noactive');
                }
            });

        $("#li-tabMaterial").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-other-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-other-noactive') > 0) {
                    $(this).removeClass('tab-other-noactive');
                    $(this).addClass('tab-other-noactive-mouse');
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-other-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-other-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-other-noactive');
                }
            });

        $("#li-tabHistory").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-other-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-other-noactive') > 0) {
                    $(this).removeClass('tab-other-noactive');
                    $(this).addClass('tab-other-noactive-mouse');
                    LoadEntryInventoryHistoryList();
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-other-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-other-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-other-noactive');
                }
            });

        $("#a-InventoryInfo").click(function () {
            if ($("#li-tabHistory").attr("class").indexOf('tab-other-active') > 0) {
                $("#li-tabHistory").removeClass('tab-other-active');
                $("#li-tabHistory").addClass('tab-other-noactive');
            }

            if ($("#li-tabHistory").attr("class").indexOf('tab-other-active') > 0) {
                $("#li-tabHistory").removeClass('tab-other-active');
            }

            $("#li-InventoryInfo").attr("data-active", true);
            $("#li-tabHistory").attr("data-active", false);
        });

        $("#a-tabHistory").click(function () {
            if ($("#li-InventoryInfo").attr("class").indexOf('tab-first-active') > 0) {
                $("#li-InventoryInfo").removeClass('tab-first-active');
                $("#li-InventoryInfo").addClass('tab-first-noactive');
            }

            if ($("#li-tabHistory").attr("class").indexOf('tab-other-active') > 0) {
                $("#li-tabHistory").removeClass('tab-other-active');
                $("#li-tabHistory").addClass('tab-other-noactive');
            }

            $("#li-tabHistory").attr("data-active", true);
            $("#li-InventoryInfo").attr("data-active", false);
        });

    </script>
}
